const TOKEN_KEY = 'access_token'
const USER_KEY = 'user'
const DEPART_ID_KEY = 'depart_id'
const DEPART_NAME = 'companyName'
// const DEPART_NAME = 'departName'
const LOGO_KEY = 'logo'
const MENUS_KEY = 'logo'

export const setToken = (token) => {
  localStorage.setItem(TOKEN_KEY, token)
}
export const setDepartName = (departName) => {
  localStorage.setItem(DEPART_NAME, departName)
}
export const setlogo = (logo) => {
  localStorage.setItem(LOGO_KEY, logo)
}
export const setMenus = (menus) => {
  console.log(menus)
  localStorage.setItem(MENUS_KEY, JSON.stringify(menus))
}

export const getToken = () => {
  const token = localStorage.getItem(TOKEN_KEY)
  return token
}
export const getMenus = () => {
  const Menu = localStorage.getItem(MENUS_KEY)
  return Menu
}


export const getDepartName = () => {
  const departName = localStorage.getItem(DEPART_NAME)
  return departName
}
export const logoImg = () => {
  const logoImg = localStorage.getItem(LOGO_KEY)
  if (!logoImg) {
    logoImg = ''
  }
  return logoImg
}
export const cleanToken = () => {
  localStorage.removeItem(TOKEN_KEY)
}

export const setUser = (user) => {
  if (!user) {
    return
  }
  localStorage.setItem(USER_KEY, JSON.stringify(user))
}
export const getUser = () => {
  let user
  try {
    user = JSON.parse(localStorage.getItem(USER_KEY))
  } catch (e) {
    console.error(e)
  }
  return user
}
export const cleanUser = () => {
  localStorage.removeItem(USER_KEY)
}

export const setDepartId = (id) => {
  localStorage.setItem(DEPART_ID_KEY, id)
}
export const getDepartId = () => {
  const departId = localStorage.getItem(DEPART_ID_KEY)
  return departId
}
export const cleanDepartId = () => {
  localStorage.removeItem(DEPART_ID_KEY)
}

export const setAuth = ({
  /* eslint-disable-next-line */
  access_token,
  departId,
  user,
  companyName,
  // departName,
  logo,
  menus,
}) => {
  setToken(access_token)
  setDepartId(departId)
  setUser(user)
  setDepartName(companyName)
  // setDepartName(departName)
  setlogo(logo)
  setMenus(menus)
}
export const cleanAuth = () => {
  cleanToken()
  cleanDepartId()
  cleanUser()
}
export const format = (time, format) => {
  var t = new Date(time);
  var tf = function(i){return (i < 10 ? '0' : '') + i};
  return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){
    switch(a){
      case 'yyyy':
      return tf(t.getFullYear());
      break;
      case 'MM':
      return tf(t.getMonth() + 1);
      break;
      case 'mm':
      return tf(t.getMinutes());
      break;
      case 'dd':
      return tf(t.getDate());
      break;
      case 'HH':
      return tf(t.getHours());
      break;
      case 'ss':
      return tf(t.getSeconds());
      break;
    }
  })
}
export function isAuth(key) {
  return JSON.parse(sessionStorage.getItem('permissions') || '[]').indexOf(key) !== -1 || false
}
